package com.example.hongyu.avtt.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.example.hongyu.avtt.R;
import com.example.hongyu.avtt.event.Event;
import com.example.hongyu.avtt.listener.OnItemClickListener;
import com.example.hongyu.avtt.model.Alive;
import com.example.hongyu.avtt.model.Vedio;
import com.example.hongyu.avtt.model.Web;
import com.example.hongyu.avtt.model.entity.BaseResponse;
import com.example.hongyu.avtt.network.BaseObserver;
import com.example.hongyu.avtt.network.RetrofitHelper;
import com.example.hongyu.avtt.network.RxUtil;
import com.example.hongyu.avtt.network.api.AppInfoApi;
import com.example.hongyu.avtt.ui.activity.PlayerActivity;
import com.example.hongyu.avtt.ui.adapter.AliveAdapter;
import com.example.hongyu.avtt.ui.adapter.WebAdapter;
import com.example.hongyu.avtt.ui.base.BaseFragment;
import com.example.hongyu.avtt.ui.widget.ToastTool;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AliveFragment extends BaseFragment {
    @BindView(R.id.recycle_view)
    XRecyclerView xRecyclerView;

    WebAdapter webAdapter;
    List<Web> webs=new ArrayList<>();
    private int pageIndex=1;
    private int pageSize=100;
   boolean isLoadData=false;
    private int dataSize=0;


    @Override
    protected View getContentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_alive, container, false);
    }

    @Override
    protected void initView() {
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
        webAdapter=new WebAdapter(this.getContext(),webs);
        xRecyclerView.setAdapter(webAdapter);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageIndex=1;
                httpGetWebList(pageIndex, true);
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                httpGetWebList(pageIndex, false);
            }
        });
        webAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastTool.show(""+position);
                Web alive=webs.get(position-1);
                String url=alive.getUrl();
                String name=alive.getName();
                Intent intent =  new Intent();
                intent.setAction( "android.intent.action.VIEW" );
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                startActivity(intent);

            }
        });
    }

    @Override
    protected void initData() {



    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFFragmentVisible(this)&&this.isAdded()){
            if(this.getParentFragment()==null||isFFragmentVisible(this.getParentFragment())){
                onLazyLoadData();
                isLoadData=true;
            }
        }
    }

    private void onLazyLoadData() {
        httpGetWebList(pageIndex, true);
    }

    @SuppressLint("AutoDispose")
    private void httpGetWebList(int index, boolean isRefresh) {
        RetrofitHelper.create(AppInfoApi.class)
                .webList("","",pageIndex,pageSize)
                .compose(RxUtil.observervableToMain())
                .subscribe( new BaseObserver<List<Web>>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<List<Web>> response) {
                        if (response!=null&&response.getData()!=null){
                            if(isRefresh){
                                webs.clear();
                                xRecyclerView.refreshComplete();
                            }else {
                                dataSize=response.getData().size();
                                xRecyclerView.loadMoreComplete();


                            }

                            webs.addAll(response.getData());
                            webAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(isRefresh){
                            xRecyclerView.refreshComplete();
                        }else {
                            pageIndex--;
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                });
    }

    private boolean isFFragmentVisible(Fragment fragment) {
        return fragment.getUserVisibleHint();
    }

    @Override
    protected void onEventProcess(Event event) {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }
}
